from django.shortcuts import render
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from home import models,homeser
from rest_framework.views import APIView
# Create your views here.
class HomeViewSet(APIView):
    def get(self,request):
        nav=models.Nav.objects.filter(flag=True,position=0).order_by('order')
        ser=homeser.HomeSerializer(nav,many=True)
        return Response({"data":ser.data})
class HomeViewSet2(APIView):
    def get(self,request):
        nav=models.Nav.objects.filter(flag=True,position=1).order_by('order')
        ser=homeser.HomeSerializer(nav,many=True)
        return Response({"data":ser.data})

class Banner(APIView):
    def get(self,request):
        banner=models.Banner.objects.filter(flag=True).order_by('order')
        ser=homeser.BannerSerializer(banner,many=True)
        return Response({"data":ser.data})
class BannerList(ListAPIView):
    queryset = models.Banner.objects.filter(flag=True).order_by("order")
    serializer_class = homeser.BannerSerializer


from django.core.cache import cache
# Create your views here.
class HeadNav(APIView):
    def get(self,request):
        #首先设置改接口的缓存key
        key="headnav"
        #去缓存中读取数据
        navlist=cache.get(key)
        if navlist:
            return Response({"code":200,"msg":"ok","data":navlist})
        #展示头部导航 有效的数据 按照order排序
        navlist=models.Nav.objects.filter(position=0,flag=True).order_by("order")
        ser=homeser.NavSerializer(navlist,many=True)
        #将数据天际到缓存中
        cache.set(key,ser.data,60*60*24)
        return Response({"code":200,"msg":"ok","data":ser.data})





